<?php 

namespace Home\Controller ; 
use Think\Controller ; 
use Think\Upload ; 

class PublicController extends Controller {


	public function sms(){


	$mobile = I('post.mobile');

                $preg = '/^1\d{10}$/'; //简单的方法
                if (!preg_match($preg, $mobile)) {
                	$this->error("手机号码格式不正确");
                }
                //查询是否有上一条数据 。
                $last_data = M('SmsLog')->where(array(
                		'mobile' => $mobile
                	))->order('id desc')->find();


                if($last_data){
                	if( $last_data['addtime'] > (time() - 60)  ){
                		$this->error('验证码获取太频繁，请稍后再试');
                	}
                }
                //查询是否已经注册 .
                $user = M('User')->where(array(
                		'username' => $mobile
                	))->find();


                if($user){
                	$this->error('手机号码已经注册，请更换手机号码');
                }

                $sms_code = get_rand_code(6);
                vendor('Alidayu.TopSdk');
                $alidayu = new \TopClient ; 
                $alidayu->appkey = C('ALIDAYU_APP_KEY');
                $alidayu->secretKey = C('ALIDAYU_SECRET_KEY') ; 

                $req = new \AlibabaAliqinFcSmsNumSendRequest;
                $req->setSmsType("normal");
                $req->setSmsFreeSignName("注册验证");
                $req->setSmsParam("{'code':'".$sms_code."','product':'123在线教育'}");
                $req->setRecNum($mobile);
                $req->setSmsTemplateCode("SMS_10875325");
                $resp = $alidayu->execute($req) ;
                $res = json_encode($resp);
                $res = json_decode($res);

                if(isset($res->result->success) && $res->result->success){
                	//发送成功 .
                	M('SmsLog')->add(array(
                		'mobile' => $mobile , 
                		'code'   => $sms_code , 
                		'addtime' => time() ,
                		'status'  => 1 ,
                		));
                	session('user_register' , array(
                		'mobile' => $mobile , 
                		'code' => $sms_code , 
                		));
                	$this->success('发送成功');
                }else{
                	$this->error("发送失败,请联系管理员");
                }

	}


        //获取省市联动
        function get_child_region($pid = NULL){
                if( ($pid != 0 && !$pid) || !is_numeric($pid)){exit;}
                $sql = "SELECT * FROM __PREFIX__region WHERE parent_id = '{$pid}'" ; 
                $region = S(md5($sql)) ;
                if(!$region){
                        $region = M()->query($sql);
                        S(md5($sql) , $region , 24 * 3600 ) ; //缓存一天 
                }
                jsonReturn(array(
                        'status' => 1 ,
                        'data'   => $region ,
                 ));
        }

        /*更改头像 */
        function changehead(){ 
                if(!is_login()){
                        exit;
                }
                $config = array(
                        'maxSize'       =>  1024 * 1024 , //上传的文件大小限制 (0-不做限制)
                        'exts'          =>  array('jpg' , 'png' , 'jpeg' , 'gif' , 'bmp'), //允许上传的文件后缀
                        'autoSub'       =>  true, //自动子目录保存文件
                        'subName'       =>  array('date', 'Y-m-d'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
                        'rootPath'      =>  './Uploads/', //保存根路径
                        'savePath'      =>  'userhead/', //保存路径
                        'saveName'      =>  array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
                        'replace'       =>  false, //存在同名是否覆盖
                        'hash'          =>  false, //是否生成hash编码
                        'callback'      =>  false, //检测文件是否存在回调，如果存在返回文件信息数组
               );
                $upload = new Upload($config);
                $info = $upload->upload();
                $return = array(
                        'status' => '1' , 
                        'url' => '' ,
                        'info' => ''
                        );

                if($info){
                        $url = $upload->rootPath . $info['userhead']['savepath'] . $info['userhead']['savename'] ;
                        $url =  substr($url, 1 , strlen($url)); 
                        $sql  = "UPDATE __PREFIX__ucenter_member SET userhead = '{$url}' WHERE id = " . is_login();
                        if(M()->execute($sql)){
                                jsonReturn(array(
                                        'status' => '1' , 
                                        'url' => $url ,
                                        'info' => '更新成功'
                                        ));
                        }else{
                               jsonReturn(array(
                                        'status' => '0' , 
                                        'url' => $url ,
                                        'info' => '更新失败'
                                        ));
                        }
                }else{
                        jsonReturn(array(
                                        'status' => '0' , 
                                        'url' => '' ,
                                        'info' => $upload->getError()
                                        ));
                }
                


        }


        //获取用户的收货地址 
        function getuserregion($id){
                if(!is_login()){exit;}
                $id = intval($id);
                $sql =  "SELECT * FROM __PREFIX__address WHERE uid = {$id}";
               
                $res = M()->query($sql);


        }


}